package com.ruoyi.web.core.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2的接口配置
 * 
 * @author ruoyi
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig
{
    @Bean
    public Docket api() {
        //添加head参数配置start 请求头
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("ticket").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        Predicate<RequestHandler> selector1 = RequestHandlerSelectors.basePackage("com.ruoyi.web.sso.controller");

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo("内部系统"))
                .groupName("内部系统")
                .select()
                .apis(Predicates.or(selector1))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);//注意这里
    }

    private ApiInfo apiInfo(String name) {
        return new ApiInfoBuilder()
                .title(name)
                .contact("荣恩网络科技有限公司")
                .description("API接口文档")
                .version("1.0")
                .build();
    }


    @Bean
    public Docket createRestApiWeb() {
        Predicate<RequestHandler> selector = RequestHandlerSelectors.basePackage("com.ruoyi.web.sso.api");

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo("外部接口"))
                .groupName("外部接口")
                .select()
                .apis(selector)
                .paths(PathSelectors.any()) //过滤的接口
                .build();
    }
}
